home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- //
- // Copyright (c) 2002, Colin Granville
- //
- // All rights reserved.
- //
- // Redistribution and use in source and binary forms, with or
- // without modification, are permitted provided that the following
- // conditions are met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- //
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following
- // disclaimer in the documentation and/or other materials
- // provided with the distribution.
- //
- // * The name Colin Granville may not be used to endorse or promote
- // products derived from this software without specific prior
- // written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- // OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- //--------------------------------------------------------------------------
-
- #include "Clip.h"
- #include "GuiBBox.h"
- #include "iostream.h"
-
-
- Clip::Clip()
- : box(0)
- {
- }
-
- //*******************************************************************
-
- Clip::~Clip()
- {
- clear();
- }
-
- //*******************************************************************
-
- void Clip::clear()
- {
- while (box) doPop();
- }
-
- //*******************************************************************
-
- int Clip::init(const GuiBBox& b)
- {
- clear();
- box=new ClipBox;
- if (box)
- {
- box->xmin=(b.xmin > b.xmax ? b.xmax : b.xmin);
- box->xmax=(b.xmax > b.xmin ? b.xmax : b.xmin);
- box->ymin=(b.ymin > b.ymax ? b.ymax : b.ymin);
- box->ymax=(b.ymax > b.ymin ? b.ymax : b.ymin);
- box->path=0;
- box->removePath=0;
- box->next=0;
- }
- return isOk();
- }
-
- //*******************************************************************
-
- void Clip::removePath()
- {
- if (isOk() && box->removePath)
- {
- delete [] box->path;
- box->path=0;
- box->removePath=0;
- }
- }
-
- //*******************************************************************
-
- void Clip::push()
- {
- if (!isOk()) return;
- ClipBox* b=new ClipBox;
- if (!b) return;
-
- *b=*box;
- b->removePath=0; // don't delete path if it was copied
- b->next=box;
- box=b;
- }
-
-
- //*******************************************************************
-
- void Clip::pop()
- {
- if (isOk() && box->next) doPop();
- }
-
- //*******************************************************************
-
- void Clip::doPop()
- {
- if (!isOk()) return;
- ClipBox* b=box->next;
- removePath();
- delete box;
- box=b;
- }
-
- //*******************************************************************
- // pathArray[0]=size
- // pathArray[1]=draw type
- // pathArray ends with 0
- void Clip::setPath(int* pathArray)
- {
- if (!pathArray) return;
-
- if (!isOk())
- {
- delete [] pathArray;
- return;
- }
-
- GuiBBox newbox;
- Boundary_start(newbox);
-
- for (int* p=pathArray+2;*p!=0;p++)
- {
- switch (*p)
- {
- case 5: break;
- case 6: Boundary_point(newbox,p[1],p[2]);
- Boundary_point(newbox,p[3],p[4]);
- Boundary_point(newbox,p[5],p[6]);
- p+=6;
- break;
- default: Boundary_point(newbox,p[1],p[2]);
- p+=2;
- break;
- }
- }
-
- //get intersection of newbox and box
- if (newbox.xmin>box->xmin) box->xmin=newbox.xmin;
- if (newbox.xmax<box->xmax) box->xmax=newbox.xmax;
- if (newbox.ymin>box->ymin) box->ymin=newbox.ymin;
- if (newbox.ymax<box->ymax) box->ymax=newbox.ymax;
-
- if (box->removePath)
- {
- //can't clip to existing path so don't change it
- // a bodge but it helps
- delete [] pathArray;
- return;
- }
-
- removePath();
- box->path=pathArray;
- box->removePath=1;
- }
-
-
-